package com.fighYo.trace.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fighYo.trace.entity.Info;
import com.fighYo.trace.entity.User;
import com.fighYo.trace.entity.query.UserQuery;
import com.fighYo.trace.entity.vo.UserVo;
import com.fighYo.trace.service.InfoService;
import com.fighYo.trace.service.PaymentService;
import com.fighYo.trace.service.UserService;
import com.fighYo.trace.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author fighYo
 * @since 2022-04-04
 */
@CrossOrigin
@Api(tags = {"用户管理"})
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private InfoService infoService;

    @Autowired
    private PaymentService paymentService;

    @ApiOperation("总数")
    @GetMapping("count")
    public R count() {
        int count = userService.count(null);
        return R.ok().data("item", count);
    }


    @ApiOperation("登录")
    @PostMapping("login")
    public R login(@RequestBody User user) {    

        String password = user.getPassword();

        user = userService.getUser(user);

        if (user == null || !user.getPassword().equals(password)) {
            return R.error().msg("用户名或密码错误");
        } else {
            return R.ok().data("token", String.valueOf(user.getId())).data("userType", user.getUserType());
        }

    }

    @ApiOperation("注册")
    @PostMapping("/register")
    public R register(@RequestBody User user) {

        User temp = userService.getUser(user);

        if (temp == null) {
            boolean save = userService.save(user);
            Info info = new Info();
            info.setUserId(String.valueOf(user.getId()));
            info.setAvatar("https://aaa-ppmall.oss-cn-hangzhou.aliyuncs.com/2022/04/12/9c215790d30644cab723d706a1a5b71edefault.png");
            infoService.save(info);
            return save ? R.ok().msg("注册成功") : R.error().msg("注册失败");
        } else {
            return R.error().msg("该用户名已存在");
        }
    }

    @ApiOperation("修改密码")
    @PostMapping("changePassword")
    public R changePassword(@RequestBody User user) {

        String password = user.getPassword();

        user = userService.getById(user.getId());
        user.setPassword(password);

        boolean result = userService.updateById(user);
        return result ? R.ok() : R.error().msg("修改失败");

    }

    @ApiOperation("修改")
    @PostMapping("/update")
    public R update(@RequestBody User user) {
        User targetUser = userService.getById(user.getId());
        if (targetUser != null) {
            boolean update = userService.updateById(user);
            return update ? R.ok().msg("修改成功") : R.error().msg("修改失败");
        } else {
            return R.error().msg("该用户不存在");
        }
    }

    @ApiOperation("获取用户")
    @GetMapping("get")
    public R getUser(HttpServletRequest request) {
        String userId = request.getHeader("access-token");
        User user = userService.getById(userId);
        user.setPassword(null);
        return R.ok().data("item", user);
    }

    @ApiOperation("获取用户分页列表")
    @PostMapping("/page/{cur}/{limit}")
    public R getUsers(@PathVariable("cur") long cur,
                      @PathVariable("limit") long limit,
                      @RequestBody UserQuery userQuery){
        IPage<UserVo> page = userService.getUserPageList(cur, limit, userQuery);

        long total = page.getTotal();
        List<UserVo> records = page.getRecords();
        return R.ok().data("total", total).data("list", records);
    }

    @ApiOperation("审批用户")
    @PostMapping("/judge")
    public R judgeUser(@RequestBody User user) {
        Integer id = user.getId();
        String judgeFlag = user.getJudgeFlag();
        user = userService.getById(id);
        user.setJudgeFlag(judgeFlag);
        boolean result = userService.updateById(user);

        UserVo ui = userService.getUserAndInfo(String.valueOf(id));

        paymentService.updateUserInfo(ui);

        return result ? R.ok() : R.error();
    }

    @ApiOperation("获取用户详细信息")
    @GetMapping("/detail")
    public R detail(String id) {
        UserVo userAndInfo = userService.getUserAndInfo(id);
        return R.ok().data("item", userAndInfo);
    }

}

